package com.example.megaandroidrssreader;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class ItemsActivity extends BaseActivity {
	
	RssSource mRssSource;
	ArrayList<RssItem> mRssItems;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_items);

		Intent intent = this.getIntent();
		mRssSource = new RssSource();
		mRssSource.mTitle = intent.getExtras().getString(BaseActivity.PACKAGE_NAME + "." + RssSource.RSS_SOURCE_TITLE); 
		mRssSource.mUrl = intent.getExtras().getString(BaseActivity.PACKAGE_NAME + "." + RssSource.RSS_SOURCE_URL); 
		
		ListView listRssItems = (ListView) this.findViewById(R.id.listRssItems);

		mRssItems = new ArrayList<RssItem>();
		RssListAdapter rssListAdapter = new RssListAdapter(this, R.layout.list_rss_item, mRssItems);
		listRssItems.setAdapter(rssListAdapter);

		// Creating AsyncTask RSS reader which will read RSS items and update RssListAdapter
		RssAsyncReader rssReader = new RssAsyncReader(ItemsActivity.this, rssListAdapter, mRssItems);
		rssReader.execute(mRssSource.mUrl);
		
		listRssItems.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
				Intent intent = new Intent(view.getContext(), RssDetailActivity.class);
				intent.putExtra(ItemsActivity.PACKAGE_NAME+".RssItem.title", mRssItems.get(position).title);
				intent.putExtra(ItemsActivity.PACKAGE_NAME+".RssItem.pubdate", mRssItems.get(position).pubDate);
				intent.putExtra(ItemsActivity.PACKAGE_NAME+".RssItem.fulltext", mRssItems.get(position).fullText);
				intent.putExtra(ItemsActivity.PACKAGE_NAME+".RssItem.fulltextUncut", mRssItems.get(position).fullTextUncut);
				intent.putExtra(ItemsActivity.PACKAGE_NAME+".RssItem.link", mRssItems.get(position).link);
				intent.putExtra(ItemsActivity.PACKAGE_NAME+".RssItem.pdalink", mRssItems.get(position).pdaLink);
				view.getContext().startActivity(intent);
			}
			
		} );
		
	}

}
